package com.prestamomo.vil.ppa.pmvilpage;

import android.app.AlertDialog;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.prestamomo.vil.ppa.BuildConfig;
import com.prestamomo.vil.ppa.R;
import com.prestamomo.vil.ppa.pmvilapi.PmvilHttpRequest;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilAtyMd;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilSpMd;
import com.prestamomo.vil.ppa.pmvilnet.pmvillistener.PmvilCallBack;
import com.prestamomo.vil.ppa.pmvilpage.pmrot.PmvilAtyRoot;

public class PmvilCofigVy extends PmvilAtyRoot {

    private ImageView configPmvilBack;
    private LinearLayout configPmvilLlout;
    private TextView configPmvilTvservi;

    @Override
    protected int getPmvilLayoutId() {
        return R.layout.cofig_pmvil_ly;
    }

    @Override
    protected void showPmvilUi() {

        configPmvilBack = findViewById(R.id.config_pmvil_iv_back);
        configPmvilLlout = findViewById(R.id.config_pmvil_ll_out);
        configPmvilTvservi = findViewById(R.id.config_pmvil_tv_version);
        configPmvilTvservi.setText("V" + BuildConfig.VERSION_NAME);
    }

    @Override
    protected void clickePmvil() {
        configPmvilBack.setOnClickListener(v -> finish());

        configPmvilLlout.setOnClickListener(v -> showOutPmvilDlg());
    }

    @Override
    protected void loadPmvilData() {

    }

    private void showOutPmvilDlg() {
        AlertDialog.Builder outPmvilBlder = new AlertDialog.Builder(this, R.style.pm_dlg_stl);
        View pmvilOutLoginView = this.getLayoutInflater().inflate(R.layout.out_pmvil_dlg, null);
        outPmvilBlder.setView(pmvilOutLoginView);
        outPmvilBlder.setCancelable(false);
        AlertDialog pmvilPermisDlg = outPmvilBlder.create();
        pmvilPermisDlg.show();

        TextView pmvileOutdlgTc = pmvilOutLoginView.findViewById(R.id.out_pmvildlg_tc);
        TextView pmvilOutdlgQx = pmvilOutLoginView.findViewById(R.id.out_pmvildlg_qx);
        pmvileOutdlgTc.setOnClickListener(v -> {
            pmvilPermisDlg.dismiss();
            outPmvilCofigLogin(BuildConfig.VERSION_NAME);
        });
        pmvilOutdlgQx.setOnClickListener(v -> pmvilPermisDlg.dismiss());
    }


    private void outPmvilCofigLogin(String pmvilVersion) {
        showPmvilLoading();
        PmvilHttpRequest.outPmvilLogin(this, pmvilVersion, new PmvilCallBack<String>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, String pmvilContent) {
                hindPmvilLoading();
                if(pmvilContent.equals("ok")){
                    PmvilSpMd.putPmvaParam(pmvilAy,"pmvil_userId_pm","");
                    PmvilSpMd.putPmvaParam(pmvilAy,"pmvil_uuId_pm","");
                    startActivity(new Intent(pmvilAy, PmvilLoginVy.class));
                    PmvilAtyMd.finishPmvilAllAty();
                }
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, String pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, String pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
    }
}
